package md.NJavaBase;

import android.util.Log;
import android.view.View;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;

//banner调度器
public class BannerDispatcher {
    //允许并行加载的Loader数量
    final int MAX_RUNING = 2;

    final String Tag = "BannerDispatcher";

    static BannerDispatcher m_I;
    public static BannerDispatcher I(){
        if(m_I!=null) return m_I;
        m_I = new BannerDispatcher();
        return m_I;
    }


    //注册一个广告
    public void RegAD(IBanner ad)
    {
        BannerLoader loader = new BannerLoader(ad);
        mLoaderList.RegAD(loader);
    }


    //在所有广告注册完毕后，管理器可以开始工作了
    public  void go(IAdLoaderCallback callback)
    {
        if(mLoaderList.size()<1) return;//没有任何广告可用

        m_callback = callback;


        //立即预装
        autoStartLoader();
        //开启主循环
        Timer reloadTimer = new Timer();// 实例化Timer类
        class  overTimeTask extends TimerTask
        {
            public void run() {
                class reloadRunnable implements Runnable
                {
                    @Override public void run() {
                        //自动补充后台预装的视频
                        autoStartLoader();
                    }
                }
                NJavaBase.getActivity().runOnUiThread(new  reloadRunnable( ));
            }
        }
        reloadTimer.schedule(new overTimeTask( ) , 1000,1000);// 毫秒
    }



    //自动启动loader
    void autoStartLoader()
    {
        long now = System.currentTimeMillis();

        //统计装载不超过20秒的数量
        //如果一个装载超过了20秒，则可以另外启动其它的loader
        int count = 0;
        for(int i=0;i<mLoaderList.size();i++)
        {
            BannerLoader curr = (BannerLoader)mLoaderList.get(i);
            if(curr.getState()== AdState.Loading&& now - curr.getStartLoadTime()<20000)
                count++;
        }

        if(count>=MAX_RUNING) {
            Log.d(Tag, "count>=MAX_RUNING");
            return;//目前运行中的loader已经达到了限制
        }

        BannerLoader idleLoader = (BannerLoader)mLoaderList.getLoader(AdState.None);//获取一个处于空闲状态的loader
        if(idleLoader==null) {
            return;//没有找到处于空闲状态的loader
        }
        //当前Loader指向下一个
        int idx = mLoaderList.getLoaderIdx(idleLoader);
        mLoaderList.moveIdx(idx);

        Log.d(Tag, "reLoad");
        //启动loader
        idleLoader.reLoad(m_callback);
    }


    //获取一个有效的视
    public BannerLoader GetActiveBanner()
    {
        ArrayList list = new ArrayList();
        for(int i=0;i<mLoaderList.size();i++)
        {
            BannerLoader curr = (BannerLoader)mLoaderList.get(i);
            View view = curr.getAD().getView();

            if(
                    view!=null    //curr.m_State == AdState.Ready&&view!=null&&!curr.isInvalidated()
            )
                list.add(curr);
        }
        if(list.size()==0) return null;
        //从列表中随机一个
        return (BannerLoader)list.get( (int) (Math.random()*list.size()) );
    }

    IAdLoaderCallback m_callback;
    LoaderList mLoaderList = new LoaderList();
}

